package com.sibco.client.business;

import java.util.HashMap;

import org.apache.commons.lang.StringUtils;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.sibco.client.common.CommUtil;
import com.sibco.client.common.Tools;
import com.sibco.client.common.MedTools;
import com.sibco.client.system.User;

/**
 * MedPayoutDatasetResultListener
 */
public class MedPayoutDatasetResultListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	  DoradoContext context = DoradoContext.getContext();  
	    User user=(User)context.getAttribute(context.SESSION,"User");
	  SqlDataset ds = (SqlDataset) dataset;
		HashMap<String, Object> map = new HashMap<String, Object>();
		String[] QueryFiled  = new String[] { 
				   "emp.EMPLOYEE_NAME",
			       "emp.EMPLOYEE_CODE",
			       "result.PRODUCT_NAME",
			       "result.VERIFY_MONEY",
			       "result.PAY_MONEY",
			       "result.VERIFY_RESULT",
			       "com.COMPANY_CHINESE_NAME",
			       "com.COMPANY_ID",
			       "result.VERIFY_ID",
			       "result.APPLY_ID",
			       "result.IS_COMPENSATE",
			       "result.IS_VALID",
			       "result.INPUT_DATE",
			       "result.INPUT_MAN",
			       "result.REF_INF",
			       "result.EMP_ID",
			       "sa.SICKNESSNAME",
			       "sa.sicktime",
			       "sa.bank_account",
			       "emp.id_card_num",
			       "sa.apply_name",
			       "sa.mobile_telephone"};
		String[] Querytables = new String[] {
				   "clt_medicare_verify_result result",
			       "clt_pb_employee emp",
			       "clt_pb_company com",
			       "clt_medicare_apply sa",
			       "clt_cs_employee_on_off_post eoop"};
		map.put("eoop.employee_id=result.emp_id", null);
		map.put("eoop.employee_id=emp.employee_id", null);
		map.put("eoop.employee_on_off_id=sa.eoop_id", null);
		map.put("eoop.COMPANY_ID=", "com.COMPANY_ID(+)");
		map.put("result.Verify_Id =","sa.Verify_Id(+)");
		if (StringUtils.isNotBlank(ds.parameters().getString("EMPLOYEE_CODE"))) {
			map.put("emp.EMPLOYEE_CODE = ", ds.parameters().getString(
					"EMPLOYEE_CODE"));
		}
		if (StringUtils.isNotBlank(ds.parameters().getString("EMPLOYEE_NAME"))) {
			map.put("emp.EMPLOYEE_NAME like ", ds.parameters().getString(
					"EMPLOYEE_NAME"));
		}
		if (StringUtils.isNotEmpty(ds.parameters().getString("PRODUCT_NAME"))
				&& StringUtils.isNotBlank(ds.parameters().getString(
						"PRODUCT_NAME"))) {
			map.put("result.PRODUCT_NAME like ", ds.parameters().getString(
					"PRODUCT_NAME"));
		}
		if (StringUtils.isNotEmpty(ds.parameters().getString("COMPANY_ID"))
				&& StringUtils.isNotBlank(ds.parameters().getString(
						"COMPANY_ID"))) {
			map.put("com.COMPANY_ID = ", ds.parameters()
					.getString("COMPANY_ID"));
		}
		if (StringUtils.isNotEmpty(ds.parameters().getString("EMPLOYEE_ID"))
				&& StringUtils.isNotBlank(ds.parameters().getString(
						"EMPLOYEE_ID"))) {
			map.put("result.EMP_ID = ", ds.parameters()
					.getString("EMPLOYEE_ID"));
		}
		if (StringUtils.isNotEmpty(Tools.format(ds.parameters()
				.getDate("BEGIN"), "yyyy-MM-dd"))) {
			map.put("to_char(result.INPUT_DATE,'yyyy-mm-dd') >= ", Tools
					.format(ds.parameters().getDate("BEGIN"), "yyyy-MM-dd"));
		}
		if (StringUtils.isNotEmpty(Tools.format(ds.parameters().getDate("END"),
				"yyyy-MM-dd"))) {
			map.put("to_char(result.INPUT_DATE,'yyyy-mm-dd') <=", Tools.format(
					ds.parameters().getDate("END"), "yyyy-MM-dd"));
		}
		if (StringUtils.isNotEmpty(ds.parameters().getString("VERIFY_RESULT"))
				&& StringUtils.isNotBlank(ds.parameters().getString(
						"VERIFY_RESULT"))) {
			map.put("result.VERIFY_RESULT =", ds.parameters().getString(
					"VERIFY_RESULT"));
		}
		String sql=MedTools.Query(map, Querytables, QueryFiled);
		sql += CommUtil.getRightSql(ds,user,"com","emp");
		String scope1=Tools.getSysdate("yyyyMMdd");
		scope1=Tools.monthCalculate(scope1, -11).substring(0,6);
		sql+=" and to_char(to_date(sa.sicktime,'yyyy-mm-dd'),'yyyymm')>='"+scope1+"'";
		ds.setSql(sql);
    return true;
  }

}
